/**
 * 
 */
package com.zaprit.search.controller;

import com.rdo.util.context.Context;
import com.rdo.util.search.FacetedSearchCriteria;
import com.rdo.util.web.service.ServiceController;
import com.rdo.util.web.service.bo.ServiceRequest;
import com.rdo.util.web.service.bo.ServiceResponse;
import com.rdo.util.web.service.exception.ServiceParameterException;
import com.zaprit.search.bo.Item;
import com.zaprit.search.exception.SearchException;
import com.zaprit.search.service.SearchService;

/**
 * @author Root
 */
public class SearchController implements ServiceController
{
	/**
	 * @param serviceRequest
	 * @param serviceResponse
	 * @throws ServiceParameterException
	 * @throws SearchException
	 */
	public void addUpdateItem(ServiceRequest serviceRequest, ServiceResponse serviceResponse)
			throws ServiceParameterException, SearchException
	{
		String scopeId = serviceRequest.getMandatoryString("scopeId", 40);
		String userId = serviceRequest.getMandatoryString("userId", 40);
		String itemId = serviceRequest.getMandatoryString("itemId", 40);

		SearchService searchService = (SearchService) Context.getBean("searchService");

		Item item = null;
		if (itemId != null)
		{
			item = searchService.getItem(scopeId, userId, itemId);
		}
		else
		{
			item = new Item();
		}

		searchService.addUpdateItem(scopeId, userId, item);
		serviceResponse.setResult(true);
	}

	/**
	 * @param serviceRequest
	 * @param serviceResponse
	 * @throws ServiceParameterException
	 * @throws SearchException
	 */
	public void searchItems(ServiceRequest serviceRequest, ServiceResponse serviceResponse)
			throws ServiceParameterException, SearchException
	{
		String scopeId = serviceRequest.getMandatoryString("scopeId", 40);
		String userId = serviceRequest.getMandatoryString("userId", 40);
		boolean requiredFacet = serviceRequest.getBoolean("requiredFacet", false);

		FacetedSearchCriteria<String> facetedSearchCriteria = new FacetedSearchCriteria<String>();

		String sortColumn = serviceRequest.getString("sortColumn", -1);
		if (sortColumn != null)
		{
			facetedSearchCriteria.setSortColumn(sortColumn);
			facetedSearchCriteria.setAscending(serviceRequest.getBoolean("ascending"));
		}
		facetedSearchCriteria.setStartIndex(serviceRequest.getInt("startIndex"));
		facetedSearchCriteria.setNoOfRecords(serviceRequest.getInt("noOfRecords"));

		int counter = 0;
		String columnParam = null;
		while ((columnParam = serviceRequest.getString("searchColumn" + ++counter, -1)) != null)
		{
			if (columnParam.startsWith("SEARCH_BY_"))
			{
				facetedSearchCriteria.addCondition(columnParam, serviceRequest
						.getMandatoryString("searchValue" + counter, -1));
			}
			else if (columnParam.startsWith("SEARCH_IN_"))
			{
				facetedSearchCriteria.addCondition(columnParam, serviceRequest
						.getMandatoryStringList("searchValue" + counter, -1));
			}
		}

		if (requiredFacet)
		{
			while ((columnParam = serviceRequest.getString("facetColumn" + ++counter, -1)) != null)
			{
				facetedSearchCriteria.getFacets().add(columnParam);
			}
		}
		SearchService searchService = (SearchService) Context.getBean("searchService");
		serviceResponse.setResult(searchService.searchItem(scopeId, userId, facetedSearchCriteria));
	}

	/**
	 * @param serviceRequest
	 * @param serviceResponse
	 * @throws ServiceParameterException
	 * @throws SearchException
	 */
	public void doRebuildIndexAll(ServiceRequest serviceRequest, ServiceResponse serviceResponse)
			throws ServiceParameterException, SearchException
	{
		SearchService searchService = (SearchService) Context.getBean("searchService");
		searchService.rebuildIndex();
		serviceResponse.setResult(true);
	}

	/**
	 * @param serviceRequest
	 * @param serviceResponse
	 * @throws ServiceParameterException
	 * @throws SearchException
	 */
	public void doRebuildIndex(ServiceRequest serviceRequest, ServiceResponse serviceResponse)
			throws ServiceParameterException, SearchException
	{
		String scopeId = serviceRequest.getMandatoryString("scopeId", 40);
		SearchService searchService = (SearchService) Context.getBean("searchService");
		searchService.rebuildIndex(scopeId);
		serviceResponse.setResult(true);
	}
}
